<?php
session_start();

if (!$_SESSION['loggedin']) {
    // User not logged in, redirect to login page
    echo "<script language='javascript'>window.location='index.php'</script>";
}
if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'registrar') {
?>
    <html>
        <head>
            <link rel="stylesheet" href="css/style.css" type="text/css" id="" media="print, projection, screen" />
            <script type="text/javascript" src="script/jquery-latest.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.pager.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    // call the tablesorter plugin
                    $("table").tablesorter({
                        // sort on the first column and third column, order asc
                        sortList: [[1,0]],
                        headers: {
                            // assign the secound column (we start counting zero)
                            0: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            6: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            }
                        }
                        ,
                        widthFixed: true, widgets: ['zebra']
                    })
                    .tablesorterPager({container: $("#pager")});
                    ;
                });

                function confirmDelete()
                {
                    var agree=confirm("Are you sure you want to deactivate professor?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }

                function confirmLogout()
                {
                    var agree=confirm("Are you sure you want to Logout?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
            </script>

            <title>TUP Professor Management</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>

        <body style=" height:100%; width: 100%; background-image: url(image/wall6.jpg);">

            <div id="container" style="height:auto; width: 100%;">

                <div style="background-image: url(image/wall12.jpg); height: 110px; width: auto; ">
                </div>

                <div align="center" style="background-image: url(image/wall11.jpg);  height: 74px; width: auto;">
                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="TMMViewProf.php"><b>[ Professor Management ]</b></a>&nbsp;&nbsp;
                        <a href="SMMViewStud.php"><b>Student Management</b></a>&nbsp;&nbsp;
                        <a href="CMMViewCourse.php"><b>Course Management</b></a>&nbsp;&nbsp;
                        <a href="EMSelectProf.php"><b>Enrollment</b></a>
                    </div>

                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="TMMAddProf.php" >Add Professor</a>&nbsp;
                        <a href="TMMViewProf.php">View Professor</a>&nbsp;
                        <a href="TMMSearchProf.php">[ Search Professor ]</a>&nbsp;
                    </div>

                    <div  align="left" style="padding-left: 4%; height: 18px; width: 46%;float:left;"><?php echo "<aa class=\"smallw\"><b>Welcome " . $_SESSION['fname'] . ", Last login: " . $_SESSION['lastlogin'] . "</b></aa>"; ?>
                    </div>
                    <div align="right" style="padding-right: 4%; height: 18px; width: 46%;float:left;"><bb>|</bb>
                        <a class="small" href="RegEditProfile.php"><b>Edit Profile</b></a>
                        <bb>|</bb>
                        <a onclick="return confirmLogout()" class="small" href="Logout.php"><b>Logout</b></a>
                        <bb>|</bb></div>
                </div>

                <div align="center" style="  float:left; height:auto; width: 100%;">

                    <h2>Search Professor</h2>

                <?php
                if (isset($_POST['Cancel'])) {
                    $idno = $_POST['prof'];
                    echo "<h3>" . $username . "</h3>";
                    include 'DBLibrary/config.php';
                    include 'DBLibrary/openDB.php';
                    mysql_select_db($dbname);

                    // $query5 = "DELETE FROM user WHERE username='" . $username . "'";
                    $query5 = "update user set validate='deactivated' WHERE idno='" . $idno . "'";

                    $queryvalue = mysql_query($query5);
                    if ($queryvalue == 0) {
                        echo "<a class=\"red\">" . mysql_errno($conn) . ": " . mysql_error($conn) . "</a>\n";
                        echo "<h3 class=\"err\">Deactivate professor failed</h3>";
                    } else if ($queryvalue == 1)
                        echo "<h3 class=\"err\">Professor has been deactivated</h3>";
                    include 'DBLibrary/closeDB.php';
                }
                ?>

                <p>You may search either by Name or Professor ID Number</p>
                <form method="post" action="" id="searchform">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="name">
                    <aa class="black"><br><input type="radio" name="nameidfil" value="flname" checked/>First Name Last Name&nbsp;&nbsp;&nbsp;
                        <input type="radio" name="nameidfil" value="idno"/>Professor ID Number
                    </aa><br><input type="submit" name="submit" value="Search">
                </form>


                <?php
                if (isset($_POST['submit'])) {
                    if (preg_match("/[A-Z | a-z | 0-9]+/", $_POST['name'])) {

                        $name = $_POST['name'];
                        list($fname, $mname, $lname) = split('[ ]', $name);
                        //connect to the database
                        include 'DBLibrary/config.php';
                        include 'DBLibrary/openDB.php';
                        mysql_select_db($dbname);

                        //-query the database table

                        if ($_POST['nameidfil'] == 'flname')
                            $sql = "SELECT * FROM user WHERE
             lcase(concat(fname,' ',lname)) LIKE lcase('%" . $fname . "%" . $mname . "%" . $lname . "%') and accounttype='professor' and validate='activated'
             or lcase(concat(fname,' ',lname)) LIKE lcase('%" . $fname . "%" . $lname . "%" . $mname . "%') and accounttype='professor'  and validate='activated'
             or lcase(concat(fname,' ',lname)) LIKE lcase('%" . $lname . "%" . $fname . "%" . $mname . "%') and accounttype='professor'  and validate='activated'
             or lcase(concat(fname,' ',lname)) LIKE lcase('%" . $lname . "%" . $mname . "%" . $fname . "%') and accounttype='professor'  and validate='activated'
             or lcase(concat(fname,' ',lname)) LIKE lcase('%" . $mname . "%" . $lname . "%" . $fname . "%') and accounttype='professor'  and validate='activated'
             or lcase(concat(fname,' ',lname)) LIKE lcase('%" . $mname . "%" . $fname . "%" . $lname . "%') and accounttype='professor'  and validate='activated'
";

                        else if ($_POST['nameidfil'] == 'idno')
                            $sql = "SELECT * FROM user WHERE validate='activated' and accounttype='professor' and idno like'%" . $name . "%'";



                        //-run the query against the mysql query function
                        $result = mysql_query($sql);

                        //-count results

                        $numrows = mysql_num_rows($result);
                        $ctr = 0;
                        echo "<p>" . $numrows . " results found for \"&nbsp;" . $name . "&nbsp;\"</p>";

                        //-create while loop and loop through result set
                ?>
                        <table  border="0" id="myTable" class="tablesorter" style="width: 95%;" align="center">
                            <thead><tr>
                                    <th>No</th>
                                    <th>Professor ID</th>
                                    <th>Last Name</th>
                                    <th>First Name</th>
                                    <th>Gender</th>
                                    <th>Email Address</th>
                                    <th>Deactivate</th>
                                </tr></thead>
                            <tfoot><tr>
                                    <th>No</th>
                                    <th>Professor ID</th>
                                    <th>Last Name</th>
                                    <th>First Name</th>
                                    <th>Gender</th>
                                    <th>Email Address</th>
                                    <th>Deactivate</th>
                                </tr></tfoot>
                            <tbody>
                        <?php
                        while ($row = mysql_fetch_array($result)) {
                            $ctr++;
                            echo "<tr><td>" . $ctr . "</td><td>" . $row['idno'] . "</td>";
                            echo "<td>" . $row['lname'] . "</td>";
                            echo "<td>" . $row['fname'] . "&nbsp;" . $row['mname'] . ".</td>";
                            echo "<td>" . $row['gender'] . "</td>";
                            echo "<td>" . $row['email'] . "</td>";
                            echo "<form onsubmit=\"return confirmDelete()\" name=\"add\" method=\"post\" action=\"\">";
                            echo "<td><input type=\"text\" name=\"prof\" size=\"1\" value=\"" . $row['idno'] . "\" style=\"visibility:hidden;\"/><input type=\"submit\" name=\"Cancel\" value=\"Deactivate\"/></td></form></tr>";
                        }
                    } else {
                        echo "<p class=\"red\">Please enter a search query</p>";
                    }
                        ?> </tbody></table> <?php
                    }
                        ?>

                    <div id="pager" class="pager" align="center" style="float:left; height: auto; width: 100%;">
                        <form>
                            <img src="addons/pager/icons/first.png" class="first"/>
                            <img src="addons/pager/icons/prev.png" class="prev"/>
                            <input readonly="readonly" type="text" class="pagedisplay"/>
                            <img src="addons/pager/icons/next.png" class="next"/>
                            <img src="addons/pager/icons/last.png" class="last"/>
                            <select class="pagesize">
                                <option selected="selected"  value="10">10</option>

                                <option value="20">20</option>
                                <option value="30">30</option>
                                <option  value="40">40</option>
                            </select>
                        </form>
                        <div id="footer" style="background-image: url(image/wall11.jpg);clear:both;text-align:center; height: 26px; width:auto;">
                            <div align="center" style=" height: 4px; width: auto;"></div>
                            <bb class ="small">Copyright &#169; 2011</bb>
                        </div>
                    </div>
                    <br>

                </div>
            </div>

        </body>
    </html>
<?php
                } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'admin') {
                    echo "<script language='javascript'>window.location='UMMViewUser.php'</script>";
                } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'professor') {
                    echo "<script language='javascript'>window.location='AMMSelectSection.php'</script>";
                }
?>